package cz.incad.psh.pshassign.server.search;

import cz.incad.psh.pshassign.server.Options;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URLEncoder;
import org.apache.commons.io.IOUtils;
//import org.apache.commons.httpclient.util.URIUtil;

/**
 *
 * @author alberto
 */
public class NuslSearch implements Search {

    private String q;

    @Override
    public String doSearch(String q) {
        try {
            String urlStr = Options.getOption("invenio", "solr") + "/select?wt=json&defType=edismax&rows=30&q=" +  URLEncoder.encode(q, "UTF-8");
            
            java.net.URL url = new java.net.URL(urlStr);
            
            InputStream is = url.openStream();
            StringWriter writer = new StringWriter();
            IOUtils.copy(is, writer, "UTF-8");
            return writer.toString();
            
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"error\": \"" + e + "\"}";
        }
    }
    
    
}
